<?php
require_once("../../../config.inc.php");
$isReloadable = true;
require_once("../../../app/_inc/include.php");
require_once("./_inc/entpFunction.php");

$errMsg = "";
$process_unit = sxREQ('pu');
if($process_unit == ""){	
	$url = $_SESSION[USEROBJ]->getHistory(0, true).
		"&msg=".urlencode(_t("missing cod. 'pu'"));
	serverRedir($url);
}
$entry_point = sxREQ('ep');
if($entry_point == ""){	
	$url = $_SESSION[USEROBJ]->getHistory(0, true).
		"&msg=".urlencode(_t("missing cod. 'ep'"));
	serverRedir($url);
}
$name = sxREQ('name');
if($entry_point == ""){	
	$url = $_SESSION[USEROBJ]->getHistory(0, true).
		"&msg=".urlencode(_t("missing cod. 'name'"));
	serverRedir($url);
}

if(sxREQ("reqtype") == "delete"){
	$errMsg .= entpRuleDel();
}
$sSQL = "SELECT * FROM entry_point_rule "
	." WHERE process_unit = '$process_unit' AND entry_point = '$entry_point' "
		." AND name = '$name'";
$objSDAO->query($sSQL, "comRule");
if (!$row = $objSDAO->fetch("comRule")){
	$url = $_SESSION[USEROBJ]->getHistory(0, true).
		"&msg=".urlencode("missing rule '$process_unit/$entry_point/$name'");
	serverRedir($url);
}

sxRDef('new_name', $row['name']);
sxRDef('description', $row['description']);
sxRDef('sequence', $row['sequence']);
sxRDef('com_rule', $row['module'].'.'.$row['com_rule_name']);
sxRDef('note', $row['note']);
sxRDef('script', $row['script']);
sxRDef('upgrade_status', $row['upgrade_status']);
sxRDef('is_sub', $row['is_sub']);

$sSQL = "SELECT * FROM com_rule ORDER BY module, name";
$objSDAO->query($sSQL, "comRule");
$aComRule = array('.'=>'-- select --');
while($row = $objSDAO->fetch("comRule")){
	$aComRule[$row["module"].$row["name"]] == $row["module"].$row["module"].' - '.$row["description"];
}

$aOpz = array("kMenu"=>'pro.pru', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
function objFrmSubm(rt){
	document.objFrm.reqtype.value = rt;
	document.objFrm.submit();
}
function confDel() {
	if (confirm("Confirm?") {
		document.location.href = "<?="entpRuleView.php?reqtype=delete&pu=$process_unit&ep=$entry_point&name=$name&xsn=".XSN?>";
	}
}
$aCmd = array("del"=>array("pag"=>"entpRuleView.php?reqtype=delete&pu=$process_unit&ep=$entry_point&name=$name&xsn=".XSN,
</script>
  
<?php
echo(bodyHTML($aOpz));

echo getEntpHeader($process_unit,$entry_point);

$aCmd = array("del"=>array("pag"=>"javascript:confDel();",
										"title"=>_t("delete"),
										"text"=>_t("delete")
										),
					"chg"=>array("pag"=>"entpRuleMod.php?reqtype=delete&pu=$process_unit&ep=$entry_point&name=$name&xsn=".XSN,
										"title"=>_t("change"),
										"text"=>_t("change")
										),
					"back"=>array()
					);
$sCmd = commandsHTML($aCmd);	
echo $sCmd;

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan='3'>Rule</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width=\"20%\">Name*</td>" .
			"<td class=\"text01\" width=\"80%\" colspan='2'>".sxREQ('new_name')."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Description*</td>" .
			"<td class=\"text01\" colspan='2'>".sxREQ('description')."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Sequence*</td>" .
			"<td class=\"text01\" colspan='2'>".sxREQ('sequence')."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Common rule</td>" .
			"<td class=\"text01\" colspan='2'>".sxREQ('com_rule')."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Is a subroutine</td>" .
			"<td class=\"text01\" colspan='2'>".sxREQ('is_sub')."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Upgrade status</td>" .
			"<td class=\"text01\" colspan='2'>".sxREQ('upgrade_status')."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Note</td>" .
			"<td class=\"text01\" colspan='2'>".sxREQ('note')."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Script*" ."</td>" 
			."<td class=\"text01\" colspan='2'>".getHtmlVisCode('script')
			."</td></tr>\n");
echo("</table>");
echo $sCmd;
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

function entpRuleDel(){
	global $objSDAO;
	$errMsg = "";
	if($errMsg != ""){	
		return $errMsg;
	}	
	$sSQL = "DELETE FROM entry_point_rule " .
			"WHERE name='".addslashes(sxGPC(strtolower($_REQUEST["name"])))."' AND " .
			"process_unit='".addslashes(sxGPC(trim($_REQUEST["process_unit"])))."' AND " .
			"entry_point='".addslashes(sxGPC(trim($_REQUEST["entry_point"])))."'";	
	
	$objSDAO->exec($sSQL);
	$url = $_SESSION[USEROBJ]->getHistory(-1, true);
	serverRedir($url);
	return 'redir err';
}
